package com.atguigu.gulimall.order.listener;

import com.atguigu.gulimall.order.config.MyMqConfig;
import com.atguigu.gulimall.order.entity.OrderEntity;
import com.atguigu.gulimall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@RabbitListener(queues = MyMqConfig.ORDER_RELEASE_ORDER_QUEUE)
public class OrderCloseListener {

    @Autowired
    private OrderService orderService;

    @RabbitHandler
    public void orderClose(OrderEntity orderEntity, Channel channel, Message message) throws IOException {
        try {
            orderService.orderClose(orderEntity);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }catch (Exception e){
            e.printStackTrace();
            // 拒绝并将其退回到队列
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }
}
